<template>
	<jk-dialog
		v-model="showDialog"
		title="修改密码"
		width="500px"
		:show-content-loading="showLoading"
		:confirm-btn-loading="saveBtnLoading"
		append-to-body
		:show-update-audit-btn="false"
		@on-cancel="onVisibleChange(false)"
		@on-visible-change="onVisibleChange"
		@on-confirm="onConfirmBtn"
	>
		<el-form ref="ruleForm" :model="ruleForm" :rules="rules" :show-message="true" label-width="100px" size="medium">
			<el-row>
				<el-col :span="22">
					<el-form-item class="" label="密码" prop="pwd">
						<el-input v-model="ruleForm.pwd" size="large" type="password" placeholder="密码" @change="onPwdChange" />
					</el-form-item>
				</el-col>
				<el-col :span="22">
					<el-form-item class="" label="确认密码" prop="pwdAgain">
						<el-input v-model="ruleForm.pwdAgain" size="large" type="password" placeholder="确认密码" @change="onPwdAgainChange" />
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
	</jk-dialog>

</template>
<script>
    import {
        resetUserPwd
    } from '@/api/system/user';
    export default {
        props: {
            userId: {
                type: [String, Number],
                default: ''
            },
            dialogState: {
                type: Boolean,
                default: false
            }
        },
        data(_this) {
            const validatePwd = (rule, value, callback) => {
                if (_this.ruleForm.pwdAgain) {
                    if (value && value === _this.ruleForm.pwdAgain) {
                        callback();
                    } else {
                        callback(new Error('两次密码不一致!'));
                    }
                } else {
                    if (value) {
                        callback();
                    } else {
                        callback(new Error('密码不能为空!'));
                    }
                }
            };
            const validatePwdAgain = (rule, value, callback) => {
                if (_this.ruleForm.pwd) {
                    if (value && value === _this.ruleForm.pwd) {
                        callback();
                    } else {
                        callback(new Error('两次密码不一致!'));
                    }
                } else {
                    if (value) {
                        callback();
                    } else {
                        callback(new Error('密码不能为空!'));
                    }
                }
            };
            return {
                showLoading: false,
                saveBtnLoading: false,
                showDialog: false,
                ruleForm: {
                    'pwd': '',
                    'pwdAgain': ''
                },
                rules: {
                    pwd: [{ required: true, validator: validatePwd, trigger: 'change' }],
                    pwdAgain: [{ required: true, validator: validatePwdAgain, trigger: 'change' }]
                }
            };
        },
        methods: {
            onPwdChange() {
                if (this.ruleForm.pwdAgain) {
                    this.$refs['ruleForm'].validate().catch(() => {});
                }
            },
            onPwdAgainChange() {
                if (this.ruleForm.pwd) {
                    this.$refs['ruleForm'].validate().catch(() => {});
                }
            },
            onConfirmBtn() {
                this.$refs['ruleForm'].validate(valid => {
                    if (valid) {
                        this.saveBtnLoading = true;
                        resetUserPwd(this.userId, this.ruleForm.pwd).then(res => {
                            this.saveBtnLoading = false;
                            if (res.data.status === 200) {
                                this.$emit('on-visible-change', false);
                                this.$store.dispatch('LogOut').then(() => {
                                    this.$router.push({ name: '/login' });
                                });
                            }
                        });
                    }
                });
            },
            // 取消按钮
            onVisibleChange(e) {
                this.$emit('on-visible-change', e);
                if (!e) {
                    this.$nextTick(() => {
                        this.$refs.ruleForm.resetFields();
                    });
                }
            }
        },
        watch: {
            dialogState(newVal) {
                this.showDialog = newVal;
            }
        }
    };
</script>
